package hichecker.ui.custom_views.requirement;

import hichecker.controller.Config;
import hichecker.controller.MainController;
import hichecker.ui.custom_views.passing.PassingPanel;
import hichecker.ui.general_views.container_panel.content.ContentPanel;
import hichecker.university.PTL;
import hichecker.university.Requirement;
import hichecker.university.UniversityPart;

import java.awt.Component;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;



public class RequirementSettings extends ContentPanel {
	private MainController controller;
	private Requirement requirement;

	public RequirementSettings(UniversityPart item, MainController controller) {
		super(item);
		this.controller = controller;
		this.requirement = (Requirement) item;

		initComponents();
	}

	/**
	 * Changes the percentag the requirement has for the modul grade
	 *
	 * @param percentage
	 */
	private void changePTLPercentage(FocusEvent e) {
		JTextField source = (JTextField) e.getSource();
		Component opposite = e.getOppositeComponent();

		if (opposite != null && source.getParent().equals(opposite.getParent())) {
			String percentage = ((JTextField) e.getSource()).getText();
			float gradePercentage = Float.parseFloat(percentage);
			((PTL) requirement).setGradePercentage(gradePercentage / 100);
			controller.fireItemUpdate();
		}
	}

	/**
	 * Inits the GUI components
	 */
	private void initComponents() {
		// Label and textfield for the requirements name
		JLabel lblName = new JLabel(Config.REQUIREMENT_NAME);
		JTextField txtName = new JTextField(requirement.getName());
		txtName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				requirement.setName(((JTextField) e.getSource()).getText());
			}
		});

		// Label and textfield for the percentage the grade has for the modul grade
		JLabel lblPercentage = new JLabel(Config.REQUIREMENT_PTL_PERCENTAGE);
		JTextField txtPercentage = new JTextField();
		if (requirement instanceof PTL) {
			txtPercentage.setText("" + ((PTL) requirement).getGradePercentage() * 100);
		} else {
			lblPercentage.setVisible(false);
			txtPercentage.setVisible(false);
		}
		// Set verifier and event that happens if verifier accepted input
		//txtPercentage.setInputVerifier(new NumberVerifier(0, 100));
		txtPercentage.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				changePTLPercentage(e);
			}
		});

		// Scrollpane with passing conditions
		JScrollPane pane = new JScrollPane(new PassingPanel(requirement));

		// Layout
		GroupLayout layout = new GroupLayout(this);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		// Horizontal
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup()
						.addGroup(layout.createSequentialGroup()
								.addGroup(layout.createParallelGroup()
										.addComponent(lblName)
										.addComponent(lblPercentage)
								)
								.addGroup(layout.createParallelGroup()
										.addComponent(txtName)
										.addComponent(txtPercentage)
								)
						)
						.addComponent(pane)
				)
		);

		// Vertical
		layout.setVerticalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup()
						.addComponent(lblName)
						.addComponent(txtName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
				)
				.addGroup(layout.createParallelGroup()
						.addComponent(lblPercentage)
						.addComponent(txtPercentage, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
				)
				.addComponent(pane)
		);

		setLayout(layout);
	}

	@Override
	public ContentPanel switchViewSettings() {
		return new RequirementView(item, controller);
	}
}
